<?php
/************************
 *  Codenova.pl Project
 *  Created by: Kamil Mrzygłód
 *
 *  (c) 2013 by Codenova.pl
 *  All Right Reserved
 ***********************/

namespace Gotcha\App\Extensions;

// File system class

use Gotcha\App\GotchaBootstrap;
use Gotcha\App\Extensions\GotchaCulturesExtension;
use Gotcha\App\Repositories\GotchaXMLRepository;
use Gotcha\App\Types\String;

class GotchaFileSystemExtension extends GotchaBootstrap {
    private static $config_repository;

    static public function CheckFileExistence(String $file, String $path) {
        if (file_exists($path.$file)) {
            return true;
        }
        return false;
    }

    static public function CheckDirExistence(String $dir) {
        if (is_dir($dir)) {
            return true;
        }
        return false;
    }

    static public function xmlFileHandler(String $file, String $key) {
        // Return handler xml file

        // Local variables
        $config = array();

        if (parent::$fatal_error === false) {
            // [BUGFIX] - Not checking file existence causes
            // fatal error called each time handler is
            // called with wrong filename
            if (self::CheckFileExistence($file, new String(GOTCHA_ROOT_DIR)) === false) {
                GotchaLogsExtension::createNewLogEntry(GotchaCulturesExtension::getTranslation(new String('REQUIRED_XML_FILE_DOESNT_EXISTS')), new String(GOTCHA_ROOT_DIR.$file));
                GotchaErrorsExtension::throwError(GotchaCulturesExtension::getTranslation(new String('REQUIRED_XML_FILE_DOESNT_EXISTS')), new String(GOTCHA_ROOT_DIR.$file));
                exit;
            }

            // Check for existence builded config repository
            if(!isset(self::$config_repository->config)) {
                $xml = simplexml_load_file(GOTCHA_ROOT_DIR.$file);

                // Push config values and names to an array
                foreach ($xml->property as $property) {
                    // In order to get easy access to property names,
                    // we have to typecast SimpleXML object into
                    // array
                    $typecasted_simplexml_object = (array)$property->attributes();

                    // Push property name and value using typecasted SimpleXML
                    // object and its @attributes key
                    $config[key($typecasted_simplexml_object['@attributes'])] = (string)$typecasted_simplexml_object['@attributes'][key($typecasted_simplexml_object['@attributes'])];
                }

                // We put builded array to a repository for
                // easier access and saving some resources
                self::$config_repository = new GotchaXMLRepository();
                self::$config_repository->config = $config;
            }

            // Make sure key exists
            if (array_key_exists((string)$key, self::$config_repository->config)) {
                // Return only pointed key
                return self::$config_repository->config[(string)$key];
            } else {
                // New log entry
                GotchaLogsExtension::createNewLogEntry(GotchaCulturesExtension::getTranslation(new String('REQUIRED_XML_KEY_DOESNT_EXISTS')), $key);
                GotchaErrorsExtension::throwError(GotchaCulturesExtension::getTranslation(new String('REQUIRED_XML_KEY_DOESNT_EXISTS')), $key);
                exit;
            }
        }
    }

    protected function processRoutesFile(String $file) {
        // Local variables
        $routes = array();
		
		if (self::CheckFileExistence($file, new String(GOTCHA_ROOT_DIR)) === false) {
			GotchaLogsExtension::createNewLogEntry(GotchaCulturesExtension::getTranslation(new String('REQUIRED_XML_FILE_DOESNT_EXISTS')), new String(GOTCHA_ROOT_DIR.$file));
			GotchaErrorsExtension::throwError(GotchaCulturesExtension::getTranslation(new String('REQUIRED_XML_FILE_DOESNT_EXISTS')), new String(GOTCHA_ROOT_DIR.$file));
			exit;
		}

        $xml = simplexml_load_file(GOTCHA_ROOT_DIR.$file);

        // Typecast whole file into array and
        // search for current request
        foreach ($xml->route as $route) {
            // Push URLs into array
            $routes[(string)$route->attributes()->url] = array('module' => (string)$route->attributes()->module, 'controller' => (string)$route->attributes()->controller, 'action' => (string)$route->attributes()->action);
        }

        // Return whole array
        return $routes;
    }
	
	protected static function processCultureFile(String $key, String $file) {
        // Local variables
        $registry = array();
		
		if (self::CheckFileExistence($file, new String(GOTCHA_ROOT_DIR)) === false) {
			GotchaLogsExtension::createNewLogEntry(GotchaCulturesExtension::getTranslation(new String('REQUIRED_XML_FILE_DOESNT_EXISTS')), new String(GOTCHA_ROOT_DIR.$file));
			GotchaErrorsExtension::throwError(GotchaCulturesExtension::getTranslation(new string('REQUIRED_XML_FILE_DOESNT_EXISTS')), new String(GOTCHA_ROOT_DIR.$file));
			exit;
		}

        $xml = simplexml_load_file(GOTCHA_ROOT_DIR.$file);

        // Typecast whole file into array and
        // search for current request
        foreach ($xml->registry as $register) {
            // Push URLs into array
            $registry[(string)$register->attributes()->id] = (string)$register->attributes()->text;
        }

        // Make sure key exists
		if (array_key_exists((string)$key, $registry)) {
			// Return only pointed key
			return $registry[(string)$key];
		} else {
			// New log entry
			GotchaLogsExtension::createNewLogEntry(new String(GotchaCulturesExtension::getTranslation(new String('REQUIRED_XML_KEY_DOESNT_EXISTS'))), $key);
			GotchaErrorsExtension::throwError(new String(GotchaCulturesExtension::getTranslation(new String('REQUIRED_XML_KEY_DOESNT_EXISTS'))), $key);
			exit;
		}
    }
}